/*
 * @Author: 火红色祥云 809880192@qq.com
 * @Date: 2022-12-12 21:09:57
 * @LastEditors: 火红色祥云 809880192@qq.com
 * @LastEditTime: 2022-12-13 20:25:44
 * @FilePath: /imx6ull-app/19_spi/19_SPI_ICM20608.c
 * @Description: 20221212重新学习SPI设备驱动框架,ICM20608设备驱动程序
 */
#include <linux/init.h>
#include <linux/module.h>

#include <linux/spi/spi.h>

#include <linux/device.h>

#include <linux/fs.h>
/*
file_operations
*/
#include <linux/cdev.h>

#include <linux/delay.h>
/*
mdelay
*/
#include <linux/slab.h>
/*
kzalloc
*/
#include <linux/ide.h>
/*
copy_to_user()
*/
#include "icm20608_reg.h"

#define DRIVER_NAME "DriverICM20608"

struct ST_ICM20608_DEV{
	int major;
	dev_t devid;
	struct cdev cdev;
	struct class *cls;
	struct device *CreatDev;

	void *spi_private_data;

	signed int gyro_x_adc;		/* 陀螺仪X轴原始值 	 */
	signed int gyro_y_adc;		/* 陀螺仪Y轴原始值		*/
	signed int gyro_z_adc;		/* 陀螺仪Z轴原始值 		*/
	signed int accel_x_adc;		/* 加速度计X轴原始值 	*/
	signed int accel_y_adc;		/* 加速度计Y轴原始值	*/
	signed int accel_z_adc;		/* 加速度计Z轴原始值 	*/
	signed int temp_adc;		/* 温度原始值 			*/
};

struct ST_ICM20608_DEV st_icm20608_dev;

/*
struct spi_message {
	struct list_head	transfers;

	struct spi_device	*spi;
	unsigned		is_dma_mapped:1;
	void			(*complete)(void *context);
	void			*context;
	unsigned		frame_length;
	unsigned		actual_length;
	int			status;
	struct list_head	queue;
	void			*state;
};
struct spi_transfer {
	const void	*tx_buf;
	void		*rx_buf;
	unsigned	len;

	dma_addr_t	tx_dma;
	dma_addr_t	rx_dma;
	struct sg_table tx_sg;
	struct sg_table rx_sg;

	unsigned	cs_change:1;
	unsigned	tx_nbits:3;
	unsigned	rx_nbits:3;
#define	SPI_NBITS_SINGLE	0x01 //1bit transfer 
#define	SPI_NBITS_DUAL		0x02 //2bits transfer 
#define	SPI_NBITS_QUAD		0x04 //4bits transfer 
	u8		bits_per_word;
	u16		delay_usecs;
	u32		speed_hz;

	struct list_head transfer_list;
};
*/
static s32 icm20608_write_reg(struct ST_ICM20608_DEV *dev, uint8_t reg,uint8_t *buf,uint8_t len)
{
	struct spi_device *spi_dev = (struct spi_device *)(dev->spi_private_data);

	struct spi_message msg;
	struct spi_transfer *tx;

	unsigned char *txdatabuf;

	int ret = -1;

	/*static inline void *kzalloc(size_t size, gfp_t flags)*/
	tx = kzalloc(sizeof(struct spi_transfer),GFP_KERNEL);
	if (!tx) return -ENOMEM;

	txdatabuf = kzalloc(sizeof(char)+len,GFP_KERNEL);
	if(!txdatabuf)
	{
		kfree(tx);
	}

	//把首个寄存器的bit8清零 ，因为是写数据
	*txdatabuf = reg &~ 0x80;
	memcpy(txdatabuf+1,buf,len);//+1指针向后偏移一个字节，第一个字节是首个寄存器值

	tx->tx_buf = txdatabuf;
	tx->len = len+1;

	/*static inline void spi_message_init(struct spi_message *m)*/
	spi_message_init(&msg);
	/*spi_message_add_tail(struct spi_transfer *t, struct spi_message *m)*/
	spi_message_add_tail(tx,&msg);
	/*int spi_sync(struct spi_device *spi, struct spi_message *message);*/
	ret  = spi_sync(spi_dev,&msg);

	kfree(tx);
	kfree(txdatabuf);

	return ret;
}

static int icm20608_read_reg(struct ST_ICM20608_DEV *dev,uint8_t reg,void *buf,uint8_t len)
{
	struct spi_device *spi_dev = (struct spi_device *)(dev->spi_private_data);

	struct spi_message msg;
	struct spi_transfer *tx;

	unsigned char txbuf[1];//发送一个寄存器
	unsigned char *rxbuf;

	int ret = -1;

	tx = kzalloc(sizeof(struct spi_transfer), GFP_KERNEL);
	if(!tx) return -ENOMEM;

	rxbuf = kzalloc(sizeof(char)*len, GFP_KERNEL);
	if(!rxbuf) 
	{
		kfree(tx);
		return -ENOMEM;
	}

	txbuf[0] = reg | 0x80; //读数据的时候寄存器地址bit置一
	tx->tx_buf = txbuf;
	tx->rx_buf = rxbuf;
	tx->len = 1+len;

	spi_message_init(&msg);
	spi_message_add_tail(tx,&msg);
	ret = spi_sync(spi_dev,&msg);

	memcpy(buf,rxbuf+1,len);

	kfree(rxbuf);
	kfree(tx);

	return ret;
}

static void icm20608_write_onereg(struct ST_ICM20608_DEV *st_dev, uint8_t reg, uint8_t value)
{
	uint8_t buf = value;

	icm20608_write_reg(st_dev, reg, &buf,1);

}

static uint8_t icm20608_read_onereg(struct ST_ICM20608_DEV *st_dev, uint8_t reg)
{
	uint8_t data = 0;

	icm20608_read_reg(st_dev, reg, &data,1);

	return data;
}

/*
 * @description	: 读取ICM20608的数据，读取原始数据，包括三轴陀螺仪、
 * 				: 三轴加速度计和内部温度。
 * @param - dev	: ICM20608设备
 * @return 		: 无。
 */
void icm20608_readdata(struct ST_ICM20608_DEV *dev)
{
	unsigned char data[14] = { 0 };
	icm20608_read_reg(dev, ICM20_ACCEL_XOUT_H, data, 14);

	dev->accel_x_adc = (signed short)((data[0] << 8) | data[1]); 
	dev->accel_y_adc = (signed short)((data[2] << 8) | data[3]); 
	dev->accel_z_adc = (signed short)((data[4] << 8) | data[5]); 
	dev->temp_adc    = (signed short)((data[6] << 8) | data[7]); 
	dev->gyro_x_adc  = (signed short)((data[8] << 8) | data[9]); 
	dev->gyro_y_adc  = (signed short)((data[10] << 8) | data[11]);
	dev->gyro_z_adc  = (signed short)((data[12] << 8) | data[13]);
}

static int icm20608_init(void)
{
	u8 value = 0xFF;

	icm20608_write_onereg(&st_icm20608_dev,ICM20_PWR_MGMT_1,0x80);
	mdelay(50);
	icm20608_write_onereg(&st_icm20608_dev,ICM20_PWR_MGMT_1,0x01);
	mdelay(50);

	value = icm20608_read_onereg(&st_icm20608_dev,ICM20_WHO_AM_I);
	printk("ICM20608 ID: %d\n",value);

	icm20608_write_onereg(&st_icm20608_dev, ICM20_SMPLRT_DIV, 0x00); 	/* 输出速率是内部采样率					*/
	icm20608_write_onereg(&st_icm20608_dev, ICM20_GYRO_CONFIG, 0x18); 	/* 陀螺仪±2000dps量程 				*/
	icm20608_write_onereg(&st_icm20608_dev, ICM20_ACCEL_CONFIG, 0x18); 	/* 加速度计±16G量程 					*/
	icm20608_write_onereg(&st_icm20608_dev, ICM20_CONFIG, 0x04); 		/* 陀螺仪低通滤波BW=20Hz 				*/
	icm20608_write_onereg(&st_icm20608_dev, ICM20_ACCEL_CONFIG2, 0x04); /* 加速度计低通滤波BW=21.2Hz 			*/
	icm20608_write_onereg(&st_icm20608_dev, ICM20_PWR_MGMT_2, 0x00); 	/* 打开加速度计和陀螺仪所有轴 				*/
	icm20608_write_onereg(&st_icm20608_dev, ICM20_LP_MODE_CFG, 0x00); 	/* 关闭低功耗 						*/
	icm20608_write_onereg(&st_icm20608_dev, ICM20_FIFO_EN, 0x00);		/* 关闭FIFO						*/
	
	return 0;
}


int icm20608_open(struct inode *nd, struct file *fp)
{
	fp->private_data = &st_icm20608_dev;
	return 0;
}
ssize_t icm20608_read(struct file *fp, char __user *buf, size_t len, loff_t *offset)
{
	signed int data[7];
	long err = 0;
	struct ST_ICM20608_DEV *dev = (struct ST_ICM20608_DEV *)(fp->private_data);

	icm20608_readdata(dev);
	data[0] = dev->gyro_x_adc;
	data[1] = dev->gyro_y_adc;
	data[2] = dev->gyro_z_adc;
	data[3] = dev->accel_x_adc;
	data[4] = dev->accel_y_adc;
	data[5] = dev->accel_z_adc;
	data[6] = dev->temp_adc;

	err = copy_to_user(buf, data, sizeof(data));

	return 0;
}
ssize_t icm20608_write(struct file *fp, const char __user *buf, size_t len, loff_t *offset)
{
	return 0;
}
int icm20608_release(struct inode *nd, struct file *fp)
{
	return 0;
}
static const struct file_operations icm20608_ops = {
	.open = icm20608_open,
	.read = icm20608_read,
	.write = icm20608_write,
	.release = icm20608_release,
};

int	icm20608_probe(struct spi_device *spi_icm20608)
{
	printk("icm20608_probe\r\n");

	//构建设备号
	if(st_icm20608_dev.major)
	{
		st_icm20608_dev.devid = MKDEV(st_icm20608_dev.major,0);
		/*register_chrdev_region(dev_t, unsigned, const char *);*/
		register_chrdev_region(st_icm20608_dev.devid,1,DRIVER_NAME);
	}
	else
	{
		/*int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);*/
		alloc_chrdev_region(&st_icm20608_dev.devid,0,1,DRIVER_NAME);
		st_icm20608_dev.major = MAJOR(st_icm20608_dev.devid);
	}

	st_icm20608_dev.cdev.owner = THIS_MODULE;
	/*void cdev_init(struct cdev *, const struct file_operations *);*/
	cdev_init(&st_icm20608_dev.cdev,&icm20608_ops);
	/*int cdev_add(struct cdev *, dev_t, unsigned);*/
	cdev_add(&st_icm20608_dev.cdev,st_icm20608_dev.devid,1);

	st_icm20608_dev.cls = class_create(THIS_MODULE,DRIVER_NAME);
	if(IS_ERR(st_icm20608_dev.cls)) return PTR_ERR(st_icm20608_dev.cls);
	//创建设备
	/*struct device *device_create(struct class *cls, struct device *parent,
			     dev_t devt, void *drvdata,
			     const char *fmt, ...);*/
	st_icm20608_dev.CreatDev = device_create(st_icm20608_dev.cls, NULL,st_icm20608_dev.devid,NULL,DRIVER_NAME);
    if(IS_ERR(st_icm20608_dev.CreatDev)) return PTR_ERR(st_icm20608_dev.CreatDev);

	spi_icm20608->mode = SPI_MODE_0;
	spi_setup(spi_icm20608);
	st_icm20608_dev.spi_private_data = spi_icm20608;

	icm20608_init();

	return 0;
}
int	imc20608_remove(struct spi_device *spi)
{
	cdev_del(&st_icm20608_dev.cdev);
	unregister_chrdev_region(st_icm20608_dev.devid,1);

	device_destroy(st_icm20608_dev.cls,st_icm20608_dev.devid);
	class_destroy(st_icm20608_dev.cls);

    return 0;
}
/*
struct spi_driver {
	const struct spi_device_id *id_table;
	int			(*probe)(struct spi_device *spi);
	int			(*remove)(struct spi_device *spi);
	void			(*shutdown)(struct spi_device *spi);
	struct device_driver	driver;
};

struct spi_device_id {
	char name[SPI_NAME_SIZE];
	kernel_ulong_t driver_data;
};

struct device_driver {
	const char		*name;
	struct bus_type		*bus;

	struct module		*owner;
	const char		*mod_name;

	const struct of_device_id	*of_match_table;

	int (*probe) (struct device *dev);
	int (*remove) (struct device *dev);
	void (*shutdown) (struct device *dev);
	int (*suspend) (struct device *dev, pm_message_t state);
	int (*resume) (struct device *dev);

};
struct of_device_id {
	char	name[32];
	char	type[32];
	char	compatible[128];
	const void *data;
};
*/

struct spi_device_id icm20608_id_table[] = {
    {"lux,icm20608",0},
    {}
};
struct of_device_id icm20608_of_match_table[] = {
    {.compatible = "lux,icm20608"},
    {}
};
struct spi_driver icm20608_driver = {
    .id_table = icm20608_id_table,
    .probe = icm20608_probe,
    .remove = imc20608_remove,
    .driver = {
        .owner = THIS_MODULE,
        .name = "icm20608_drivre_name",
        .mod_name = "icm20608_driver_mod_name",
        .of_match_table = icm20608_of_match_table,
    },
};



static int __init ICM20608_INIT(void)
{
    /*int spi_register_driver(struct spi_driver *sdrv);*/
    spi_register_driver(&icm20608_driver);
    return 0;
}

static void __exit ICM20608_EXIT(void)
{
    /* void spi_unregister_driver(struct spi_driver *sdrv)*/
    spi_unregister_driver(&icm20608_driver);
}

module_init(ICM20608_INIT);
module_exit(ICM20608_EXIT);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Lux");
MODULE_DESCRIPTION("20221212重新学习SPI设备驱动框架,ICM20608设备驱动程序");

